<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<script type="text/javascript" src="<s:url value='/ui/autocomplete/jquery.autocomplete.js' />"></script>
<link href="<s:url value='/ui/autocomplete/css/jquery.autocomplete.css'/> " rel="stylesheet" type="text/css" />
<script type="text/javascript" >
var editor="null";
var treeUrl = ctx + '/sm/User!findUserTree.action';
var attrFiles = [];
$(function(){
	$('#sendPerson').focus();
	$('#preview').bind('focus',function(){
		if('null'==editor){
			editor = KindEditor.create('#preview', {
				basePath:ctx+'/ui/kindeditor/',
			    uploadJson : ctx + '/ui/kindeditor/jsp/upload_json.jsp',
			    fileManagerJson : ctx + '/ui/kindeditor/jsp/file_manager_json.jsp',
			    allowFileManager : true
			    //items:[
			    //       'source','|', 'preview', 'cut', 'copy', 'paste',
			    //       'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
			    //       'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent','outdent','quickformat',
			    //       'selectall','formatblock','fontname','fontsize','forecolor','hilitecolor','bold','italic'
			    //]
			});
		}
	});
	$('#writeMail').bind('click',function(){
		//让 编辑器吧数据保存在文本域中
		if('null'!=editor){
			editor.sync();
		}
		$('#fileIds').val(attrFiles);
		CQOA.formSubmit($('#sendMailFrom'),ctx+'/pof/Mail!send.action',function(o){
			if(o.success){
				CQOA.loadMain(ctx+'/pof/Mail!outbox.action',{},function(){});
			}
			$.messager.show({
				msg : o.msg
			});
		},{});
	});
	
	$('#saveMail').bind('click',function(){
		$('#sendPerson').val('this is email');
	});
	
	$('#sendPerson').bind('focus',function(){
		$("#reciveType").val('sendPerson');
	});
	
	
	$('#orgUserTree').tree({
		url: treeUrl,
        fit: true,
        idField: 'id',
        treeField: 'text',
        onBeforeLoad : function(row, param) {
			if (row) {
				treeUrl = ctx + '/sm/User!findUserTree.action?id='+row.id;
			}
		},
		onDblClick : function(node){
			
		}
	});
	
	
	$('#uploadFile').bind('click',function(){
			  //默认是附件
		    var isTypes = 2;
		    var targetTableSid = "";
		    //0 正式  1 临时
		    var isTempFile = 1;
		    var attachmentType = "doc|xls|gif|txt|zip|rar|jpg|pdf";//附件类型
		    var attachmentNumber =10;
		    var attachmentSize = 20;
		    var values = 1024 * 1024;
		    /**
		     * 文件上传菜单
		     * @param {Object} businessObject 业务对象名
		     * @param {Object} targetTableSid 业务数据SID(OPTIONAL)
		     * @param {Object} types 附件类型
		     * @param {Object} isTempFile 是否临时文件
		     * @param {Object} url 上传的URL
		     * @param {Object} allowedFileTypes  允许的文件内容,以|分隔 gif|jpg
		     * @param {Object} allowedSingleFileSize 允许上传的个文件大小
		     * @param {Object} callback 回调function
		     * @param {Object} maxLength 允许上传的文件数
		     */
		    fn_fileupload("Mail", targetTableSid, isTypes, isTempFile, attachmentType, attachmentSize * values, attachmentNumber, function(data){
		    	for (var i = 0; i < data.length; i++) {
		    		var downLoadUrl = '<a href="#" onClick="downloadFun('+data[i].sid+');" style="color:blue;">' + data[i].cname + '</a>';
		    		var delUrl = '<a href="#" onClick="deleteAttr('+data[i].sid+');" style="color:blue;">【删除】</a>';
		    		$('#attrIds').append("<p id='attache" + data[i].sid + "'>" + downLoadUrl + " " + delUrl + "</p>");
		    		attrFiles.push(data[i].sid);
		    	}
		    });
	});
	
	
	//本单位搜索
    $("#sendPerson").autocomplete(ctx + '/sm/User!findEmail.action', {
        minChars: 1,
        width: 220,
        cacheLength: 1,
        matchContains: true,
        autoFill: false,
        matchSubset: false,
        multiple: true,
        postType: 'choose',
        onSelected: function(data){
        	setEmail("\"" + data.cname + "\" <" + data.email + ">");
        },
        parse: function(data){//解析JSON数据
            var rows = [];
            for (var i = 0; i < data.length; i++) {
            	//如果有电子邮件地址的，则加入row
            	if(!data[i].email == ""){
                    rows[rows.length] = {
                        data: data[i],
                        value: data[i].email,
                        result: "\"" + data[i].cname + "\" <" + data[i].email + ">"
                    };
            	}
            }
            return rows;
        },
        formatItem: function(row, i, max){
        	return "\"" + row.cname + "\"--" + row.email;
        }
    });
	
	
});

//在收件栏中添加选择的email
function setEmail(newEmail){
	
};

/**
 * 删除附件信息
 */
function deleteAttr(id){
	deleteFile(id,function(){
		 $("#attache" + id).remove();
		 attrFiles.remove(id);
	});
};


</script>
<div id="mainDiv" class="easyui-layout" fit="true" border="false">
	        <div id="easyui-title" style="display:none">
	            	写邮件
	        </div>
           <div  region="center" border="false">
            	<div class="easyui-layout" fit="true" border="false">
            		 <div  region="center" border="false">
            		 	  <div style="text-align: center;height: 30px;">
            		 	  	 <a href="#" class="easyui-linkbutton"  id="writeMail">发送邮件</a>
                   		     <a href="#" class="easyui-linkbutton"  id="saveMail">保存邮件</a>
            		 	  </div>
            		 	<s:form method="post" name="mail" id="sendMailFrom" style="height:99%">
            		 	  <s:hidden id="fileIds" name="attrId"></s:hidden>
            		 	  <s:hidden id="reciveType" ></s:hidden>
	            	 	  <table class="dataTable">
				          		<tr>
				          			<td width="120px;">收件人</td>
				          			<td>
				          				<s:textarea onkeypress="if(event.keyCode==13 || event.keyCode==32) return false;" name="entity.sendTo" type="text"  id="sendPerson" style="width: 95%;" rows="2"  cssClass="easyui-validatebox"  validType="length[1,3000]"  notnull="true" />
				          			</td>
				          		</tr>
				          		<tr>
				          			<td>抄送</td>
				          			<td><s:textfield id="copyPerson" name="entity.copyTo" cssClass="easyui-validatebox"  validType="length[1,3000]"  style="width:95%" /></td>
				          		</tr>
				          		<tr>
				          			<td>密送</td>
				          			<td><s:textfield id="bccPerson" name="entity.bccTo" cssClass="easyui-validatebox"  validType="length[1,3000]"  style="width:95%" /></td>
				          		</tr>
				          		<tr>
				          			<td>标题</td>
				          			<td><s:textfield name="entity.title" cssClass="easyui-validatebox"  validType="length[1,700]" notnull="true"  style="width:95%" /></td>
				          		</tr>
				          		<tr>
				          			<td>上传附件</td>
				          			<td>
				          				 <a href="#" class="easyui-linkbutton" id="uploadFile">上传附件</a>
				          				 <p id="attrIds"></p>
				          			</td>
				          		</tr>
				          		<tr>
				          			<td colspan="2">
				          				<s:if test="entity.backStatus==1">
		                                    <input type="checkbox" name="entity.backStatus" id="isSendBack" value="1" checked="checked"/>
		                                </s:if>
		                                <s:else>
		                                    <input type="checkbox" name="entity.backStatus" id="isSendBack" value="1"/>
		                                </s:else> 要求回执
		                                <s:if test="entity.isUrgent==1">
		                                    <input type="checkbox" id="urgent" name="entity.isUrgent" value="1" checked="checked"/>
		                                </s:if>
		                                <s:else>
		                                    <input type="checkbox" id="urgent" name="entity.isUrgent" value="1" />
		                                </s:else> 紧急
				          			</td>
				          		</tr>
				          		<tr>
				          			<td width="120px;">正文</td>
				          			<td>
				          				<s:textarea id="preview" name="entity.msg" cssClass="easyui-validatebox"  validType="length[1,5000]" rows="20" style="width: 95%;"/>
				          			</td>
				          		</tr>
				          </table>
				          </s:form>
			          </div>
		          	  <div region="east" style="width: 160px;" title="联系人">
		          	  		<ul id="orgUserTree" class="easyui-tree"></ul>
		          	  </div>
		        </div> 
           </div>
</div>
<div id="extendDiv" class="easyui-panel" style="display:none;" fit="true" border="false"></div>


